package com.itany.shop.servlet;

import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 石小俊
 */
@WebServlet("*.product")
public class ProductServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/findAll.product":
                findAll(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请检查请求</h1>");
                break;
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService productService = (ProductService) ObjectFactory.getObject("productService");

        List<Product> products = productService.findAll();
        request.setAttribute("products", products);
        request.getRequestDispatcher("/jsp/main.jsp").forward(request, response);
    }
}
